<template>
  <basic-container shadow="hover">
    <curd-title :title="'风险对应措施'" />
    <avue-crud
      :table-loading="loading"
      :option="option"
      :data="data"
      @on-load="onLoad"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @refresh-change="refreshChange"
      @row-del="rowDel"
    >
      <template slot="measuresLevelForm">
        <el-tag :type="['primary', 'danger', 'warning', 'success'][form.measuresLevel]">
          <!-- {{ scope.row.riskLevel_dictText }} -->
          {{ ['Y', 'R', 'A', 'G'][form.measuresLevel] }}
        </el-tag>
      </template>
      <template slot="measuresLevel" slot-scope="scope">
        <el-tag :type="['primary', 'danger', 'warning', 'success'][scope.row.measuresLevel]">
          <!-- {{ scope.row.riskLevel_dictText }} -->
          {{ ['Y', 'R', 'A', 'G'][scope.row.measuresLevel] }}
        </el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { option } from '@/option/tenant/supplier/measure'
export default {
  name: 'RiskMeasures',
  inject: ['type'],
  data() {
    return {
      form: {},
      data: [
        {
          measuresLevel: '0'
        }
      ],
      loading: false,
      option: option.call(this)
    }
  },
  computed: {
    getRiskLevel() {
      return {
        p: this.form.measuresPossibility,
        r: this.form.measuresResult
      }
    }
  },
  watch: {
    getRiskLevel(val) {
      if (val.p === '0') {
        if (val.r === '0') return (this.form.measuresLevel = '3')
        if (val.r === '1') return (this.form.measuresLevel = '3')
        if (val.r === '2') return (this.form.measuresLevel = '0')
        if (val.r === '3') return (this.form.measuresLevel = '0')
        if (val.r === '4') return (this.form.measuresLevel = '2')
      }
      if (val.p === '1') {
        if (val.r === '0') return (this.form.measuresLevel = '3')
        if (val.r === '1') return (this.form.measuresLevel = '0')
        if (val.r === '2') return (this.form.measuresLevel = '0')
        if (val.r === '3') return (this.form.measuresLevel = '2')
        if (val.r === '4') return (this.form.measuresLevel = '2')
      }
      if (val.p === '2') {
        if (val.r === '0') return (this.form.measuresLevel = '0')
        if (val.r === '1') return (this.form.measuresLevel = '0')
        if (val.r === '2') return (this.form.measuresLevel = '2')
        if (val.r === '3') return (this.form.measuresLevel = '2')
        if (val.r === '4') return (this.form.measuresLevel = '1')
      }
      if (val.p === '3') {
        if (val.r === '0') return (this.form.measuresLevel = '0')
        if (val.r === '1') return (this.form.measuresLevel = '2')
        if (val.r === '2') return (this.form.measuresLevel = '2')
        if (val.r === '3') return (this.form.measuresLevel = '1')
        if (val.r === '4') return (this.form.measuresLevel = '1')
      }
      if (val.p === '4') {
        if (val.r === '0') return (this.form.measuresLevel = '2')
        if (val.r === '1') return (this.form.measuresLevel = '2')
        if (val.r === '2') return (this.form.measuresLevel = '1')
        if (val.r === '3') return (this.form.measuresLevel = '1')
        if (val.r === '4') return (this.form.measuresLevel = '1')
      }
    }
  },
  methods: {
    onLoad() {
      // this.loading = true
      // getFactoryList({ supplierId: this.supplierId }).then(res => {
      //   this.data = res.data.data.records
      //   this.loading = false
      // })
    },
    refreshChange() {
      this.onLoad()
    },
    rowSave(row, done, loading) {
      console.log(this.form)
      // addFactory({ ...row, supplierId: this.supplierId }).then(
      //   () => {
      //     this.onFactoryLoad()
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!'
      //     })
      //     done()
      //   },
      //   error => {
      //     window.console.log(error)
      //     loading()
      //   }
      // )
    },
    rowUpdate(row, index, done, loading) {
      // updateFactory(row).then(
      //   () => {
      //     this.onFactoryLoad()
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!'
      //     })
      //     done()
      //   },
      //   error => {
      //     window.console.log(error)
      //     loading()
      //   }
      // )
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // return removeFactory(row.id)
        })
        .then(() => {
          this.onFactoryLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
